import {findMenuByModelAndRoleName} from '@/api/sidebar.js'
import portalInit from '@/assets/js/portalInit'
import Message from 'element-ui'
// import {async} from 'q'
export default {
  components: {
    portalInit,
    Message
    // async
  },
  namespaced: true,
  state: {
    allApp: [
      {
        'id': 1,
        'name': '配置中心',
        'roleName': null,
        'userName': null,
        'planeModel': null,
        'childrenMenuList': [
          {
            'name': '历史版本',
            'url': '/version'
          },
          {
            'name': '用户权限组管理',
            'url': '/roleGroup'
          },
          {
            'name': '字典管理',
            'url': '/api/dict/list'
          }
        ]
      }
    ]
  },
  getters: {},
  mutations: {
    setAllApp (state, payload) {
      // state.allApp = payload TODO
    }
  },
  actions: {
    async getAllApp ({
      commit,
      rootState
    }, payload) {
      let {
        User
      } = rootState
      let params = {
        modelName: User.aircraftModel,
        roleName: User.roleName
      }
      await findMenuByModelAndRoleName(params).then(res => {
        res.forEach(item => {
          if (item.name === '文件管理(C919)' || item.name === '文件管理' || item.name === '操作入口' || item.name === '操作入口(C919)' || item.name === '文件管理(ARJ21)' || item.name === '文件管理（CXF）') {
            let childrenMenuList = item.childrenMenuList
            for (let i = 0; i < childrenMenuList.length; i++) {
              if (childrenMenuList[i].name === '新发技术文件') {
                childrenMenuList[i].name = '新发技术文件(新)'
              }
              if (childrenMenuList[i].name === '新发试飞大纲') {
                childrenMenuList[i].name = '新发试飞大纲(新)'
              }
            }
          }
          // if (item.name == '文件管理(ARJ21)' || item.name == '文件管理（CXF）') {
          //   let childrenMenuList = item.childrenMenuList
          //   for (let i = 0; i < childrenMenuList.length; i++) {
          //     if (childrenMenuList[i].name == '新发技术文件') {
          //       var newItem = []
          //       newItem.name = '新发技术文件(仅支持ie11)'
          //       newItem.url = childrenMenuList[i].url
          //       childrenMenuList.splice(i, 1, newItem)
          //     }
          //   }
          // }
          if (item.name === '产品结构(C919)' || item.name === '产品结构' || item.name === '文件管理' || item.name === '文件管理(C919)') {
            let childrenMenuList = item.childrenMenuList
            for (let i = 0; i < childrenMenuList.length; i++) {
              if (childrenMenuList[i].name === '新发文件') {
                var newItem = []
                newItem.name = '新发技术文件(新)'
                newItem.url = childrenMenuList[i].url
                childrenMenuList.splice(i + 1, 0, newItem)
              }
              if (childrenMenuList[i].name === 'C919飞机定义视图') {
                childrenMenuList[i].name = 'C919飞机定义视图查询(新)'
                newItem = []
                newItem.name = 'C919飞机定义视图(原)'
                newItem.url = childrenMenuList[i].url
                childrenMenuList.splice(i + 1, 0, newItem)
              }
              if (childrenMenuList[i].name === 'C919飞机设计视图') {
                childrenMenuList[i].name = 'C919飞机设计视图查询(新)'
                newItem = []
                newItem.name = 'C919飞机设计视图(原)'
                newItem.url = childrenMenuList[i].url
                childrenMenuList.splice(i + 1, 0, newItem)
              }
              if (childrenMenuList[i].name === 'C919飞机系统视图') {
                childrenMenuList[i].name = 'C919飞机系统视图查询(新)'
                newItem = []
                newItem.name = 'C919飞机系统视图(原)'
                newItem.url = childrenMenuList[i].url
                childrenMenuList.splice(i + 1, 0, newItem)
              }
              if (childrenMenuList[i].name === 'C919飞机试验视图') {
                childrenMenuList[i].name = 'C919飞机试验视图查询(新)'
                newItem = []
                newItem.name = 'C919飞机试验视图(原)'
                newItem.url = childrenMenuList[i].url
                childrenMenuList.splice(i + 1, 0, newItem)
              }
              if (childrenMenuList[i].name === 'C919飞机选配视图') {
                childrenMenuList[i].name = 'C919飞机选配视图查询(新)'
                newItem = []
                newItem.name = 'C919飞机选配视图(原)'
                newItem.url = childrenMenuList[i].url
                childrenMenuList.splice(i + 1, 0, newItem)
              }
            }
          }
          if (item.name.indexOf('工程更改') === 0) {
            let childrenMenuList = item.childrenMenuList
            for (let i = 0; i < childrenMenuList.length; i++) {
              if (childrenMenuList[i].name === '工程更改建议') {
                childrenMenuList[i].name = '工程更改建议(仅支持IE11)'
              }
            }
          }
        })
        commit('setAllApp', res)
      })
    }
  }
}
